Package Question4_1

Source Code of Question4_1.QuestionImproved

package Question4_1;
import CtCILibrary.TreeNode;

public class QuestionImproved {
   
  public static int checkHeight(TreeNode root) {
    if (root == null) {
      return 0;
    }
    int leftHeight = checkHeight(root.left);
    if (leftHeight == -1) {
      return -1;
    }
    int rightHeight = checkHeight(root.right);
    if (rightHeight == -1) {
      return -1;
    }
   
    int heightDiff = leftHeight - rightHeight;
    if (Math.abs(heightDiff) > 1) {
      return -1;
    }
    else {
      return Math.max(leftHeight, rightHeight) + 1;
    }
  }
 
  public static boolean isBalanced(TreeNode root) {
    if (checkHeight(root) == -1) {
      return false;
    } else {
      return true;
    }
  }
 
  public static void main(String[] args) {
    // Create balanced tree
    int[] array = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10};
    TreeNode root = TreeNode.createMinimalBST(array);

   
    System.out.println("Is balanced? " + isBalanced(root));
   
    root.insertInOrder(4); // Add 4 to make it unbalanced

    System.out.println("Is balanced? " + isBalanced(root));
  }

}
TOP

Related Classes of Question4_1.QuestionImproved

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.